class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def numComponents(self, head, nums):
        """
        :type head: ListNode
        :type nums: List[int]
        :rtype: int
        """
        nums = set(nums)
        state = 0
        c = 0
        while head:
            if head.val in nums:
                if not state:
                    c += 1
                    state = 1
            else:
                state = 0
            head = head.next
        return c

